package jmu.cyxwcy.librarymanagement.controller;


import jmu.cyxwcy.librarymanagement.pojo.BookDescription;
import jmu.cyxwcy.librarymanagement.pojo.College;
import jmu.cyxwcy.librarymanagement.pojo.Result;
import jmu.cyxwcy.librarymanagement.service.BookDescriptionService;
import jmu.cyxwcy.librarymanagement.service.ICollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2024-06-26
 */
@RestController
@RequestMapping("/college")
//学院
public class CollegeController {
    @Autowired
    private ICollegeService iCollegeService;

    @PostMapping("/add")
    public Result add(@RequestBody College college){
        iCollegeService.getBaseMapper().insert(college);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody College college){
        iCollegeService.updateById(college);
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result delete(Integer collegeId){
        iCollegeService.removeById(collegeId);
        return Result.success();
    }

    @GetMapping("/list")
    public Result<List<College>> getAllColleges() {
        List<College> colleges = iCollegeService.list();
        return Result.success(colleges);
    }

    @GetMapping("/findbycollegenamepartial")
    public Result findBookDescriptionByPartialIsbn(@RequestParam String collegeNamePartial) {
        List<College> Colleges = iCollegeService.findByCollegeNamPartial(collegeNamePartial);
        return Result.success(Colleges);
    }

    @GetMapping("/findidbyname")
    public Result findIdByCollegeName(@RequestParam String collegeName) {
        Integer collegeId = iCollegeService.findIdByCollegeName(collegeName);
        if (collegeId != null) {
            return Result.success(collegeId);
        } else {
            return Result.error("未找到对应的学院ID");
        }
    }

}
